package com.gcxy.jk.shixunyi.service;

import com.gcxy.jk.shixunyi.dao.OrderMapper;
import com.gcxy.jk.shixunyi.dao.PaymentMapper;
import com.gcxy.jk.shixunyi.dao.UserMapper;
import com.gcxy.jk.shixunyi.dto.PaymentDto;
import com.gcxy.jk.shixunyi.entity.Order;
import com.gcxy.jk.shixunyi.entity.Payment;
import com.gcxy.jk.shixunyi.entity.User;
import com.gcxy.jk.shixunyi.enums.OrderStatusEnum;
import com.gcxy.jk.shixunyi.utils.ArithmeticUtils;
import com.gcxy.jk.shixunyi.utils.GetNowTime;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.StringValue;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author xujianlun
 * @Date 2022/10/1 20:49
 * @注释 用户支付信息
 */
@Service
@Slf4j
public class PaymentService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PaymentMapper paymentMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 创建支付信息
     * @param orderId
     * @param
     * @param createTime
     * @param
     * @param openid
     * @return
     *///BigDecimal payAmount,Long createUser
    @Transactional
    public String createPayment(Long orderId,String openid ){
        String pass = "成功";
        String error = "失败！余额不足";
        String error1 = "失败！用户不在";
        //判断是否有用户存在
        if (StringUtils.isBlank(openid)){
            return error1;
        }else {
            //查询到此用户信息
            Example example = Example.builder(User.class).build();
            example.createCriteria().andLike("openid",openid);
            User user = userMapper.selectOneByExample(example);
            //用于生成订单信息的

            BigDecimal userMoney = user.getMoney();
            //查询到·11关于订单信息的 orderid
//            Example orderExample = Example.builder(Order.class).build();
//            orderExample.createCriteria().andLike("orderId", String.valueOf(orderId));

            Order order = orderMapper.selectByPrimaryKey(orderId);
            BigDecimal orderMoney = BigDecimal.valueOf(order.getAmount());
            //和用户余额比较 如果用户余额不足，返回
            if (Double.parseDouble(String.valueOf(orderMoney)) > Double.parseDouble(String.valueOf(userMoney))){
                return error;
            }else {
                //创建用户支付信息模拟支付   要开启java事务才安全有效 springboot 事务注解：@Transactional
                Long uid = user.getId();
                //调用运算类进行减法运算
                // 有优惠券可以处理优惠券折扣
                BigDecimal result2 = BigDecimal.valueOf(ArithmeticUtils.sub(userMoney,orderMoney));


                user.setMoney(result2);//设置插入数据
                userMapper.updateByExampleSelective(user,example);

                Payment payment = new Payment();
                payment.setOrderId(orderId);
                payment.setPayAmount(orderMoney);
                //设置当前的时间存入数据库
                payment.setCreateTime(GetNowTime.getNowTime());
                payment.setUpdateTime(GetNowTime.getNowTime());

                payment.setCreateUser(uid);
                payment.setUpdateUser(uid);
                //订单支付
                Example example2 = Example.builder(Order.class).build();
                example2.createCriteria().andLike("id", String.valueOf(orderId));

                order.setStatus(OrderStatusEnum.FINISH);
                orderMapper.updateByExampleSelective(order,example2);

                paymentMapper.insert(payment);
            }
        }
        return pass;
    }

//    /**
//     * 用户订单信息详情
//     * @param id
//     * @return
//     */
//    public List<Map<String,Object>>  paymentDetail(Long id){
//
//    }

    /**
     * 所以支付订单信息
     * @param
     * @return
     */
        public  List<Payment> findAll(){
            List<Payment> payments = paymentMapper.selectAll();
            return payments;
        }
}

